package com.offcn.webui.controller;

import com.offcn.dycommon.response.AppResponse;
import com.offcn.webui.service.ProjectServiceFeign;
import com.offcn.webui.service.UserServiceFeign;
import com.offcn.webui.vo.resp.ProjectDetailVo;
import com.offcn.webui.vo.resp.ReturnPayConfirmVo;
import com.offcn.webui.vo.resp.UserAddressVo;
import com.offcn.webui.vo.resp.UserRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.util.List;

/**
 * @Auther: yao
 * @Date: 2020/10/28 11:31
 * @Description:
 */
@Controller
@RequestMapping("/project")
public class ProjectController {


    @Autowired
    private ProjectServiceFeign projectServiceFeign;

    @Autowired
    private UserServiceFeign userServiceFeign;


    @RequestMapping("/projectInfo")
    public String findProjectInfo(Integer id, Model model, HttpSession session) {
        //1.远程调用服务得到项目详情
        AppResponse<ProjectDetailVo> response = projectServiceFeign.findProjectById(id);
        ProjectDetailVo projectDetailVo = response.getData();
        //2.设置对象
        model.addAttribute("DetailVo", projectDetailVo);
        session.setAttribute("DetailVo", projectDetailVo);
        return "/project/project";
    }


    @RequestMapping("/confirm/returns/{projectId}/{returnId}")
    public String findReturnInfo(@PathVariable("projectId") Integer projectId, @PathVariable("returnId") Integer returnId, Model model, HttpSession session) {
        //1.获得项目详情信息，从session获得
        ProjectDetailVo projectDetailVo = (ProjectDetailVo) session.getAttribute("DetailVo");
        //2.查询项目回报详情信息

        AppResponse<ReturnPayConfirmVo> response = projectServiceFeign.findTReturnById(returnId);
        ReturnPayConfirmVo returnPayConfirmVo = response.getData();
        returnPayConfirmVo.setProjectId(projectId);                 //项目ID
        returnPayConfirmVo.setProjectName(projectDetailVo.getName());           //项目名称

        //3.根据会员编号查询用户的详情
        AppResponse<UserRespVo> userRespVoAppResponse = userServiceFeign.findMemberById(projectDetailVo.getMemberid());
        UserRespVo userRespVo = userRespVoAppResponse.getData();
        returnPayConfirmVo.setMemberName(userRespVo.getRealname());

        //4.设置对象
        model.addAttribute("returnConfirm", returnPayConfirmVo);
        session.setAttribute("returnConfirm", returnPayConfirmVo);

        return "/project/pay-step-1";
    }

    @RequestMapping("/confirm/order/{num}")
    public String confirmOrder(@PathVariable(name = "num") Integer num,Model model, HttpSession session) {
        //1.从session中获得用户对象
        UserRespVo userRespVo = (UserRespVo) session.getAttribute("sessionMember");
        if (userRespVo == null) {
            //如果登录超时，则跳转回登录页面，并且将当前的请求地址设置到session，方便登录后直接跳转
            session.setAttribute("preUrl", "/project/confirm/order/" + num);
            return "rediect:/login.html";
        }
        //2.取得登录用户的地址列表
        AppResponse<List<UserAddressVo>> response = userServiceFeign.findAddressList(userRespVo.getAccessToken());
        List<UserAddressVo> userAddressVoList = response.getData();
        //3.将取得的地址列表直接设置到model中
        model.addAttribute("addresses",userAddressVoList);

        //4.从session中获得回报确认对象
        ReturnPayConfirmVo returnPayConfirmVo = (ReturnPayConfirmVo) session.getAttribute("returnConfirm");
        returnPayConfirmVo.setNum(num);             //购买数量

        returnPayConfirmVo.setTotalPrice(new BigDecimal(num*returnPayConfirmVo.getSupportmoney()+returnPayConfirmVo.getFreight()));         //总价
        session.setAttribute("returnConfirmSession",returnPayConfirmVo);
        return "/project/pay-step-2";
    }


}
